<template>
  <view>
    <!-- 预留出的搜索区域 -->
    <MySearch @click="goToSearchPage"></MySearch>
    <view class="scroll-view-container">
      <!-- 左侧的滚动区域 -->
      <scroll-view
        class="left-scroll-view"
        scroll-y
        :style="{ height: wh + 'px' }"
      >
        <view
          v-for="(item, index) in categoryList"
          :key="item.category_id"
          class="left-scroll-view-item"
          :class="{ active: index === selectedCategory }"
          @click="selectCategoryHandler(index)"
          >{{ item.name }}</view
        >
      </scroll-view>

      <!-- 右侧的滚动区域 -->
      <scroll-view
        class="right-scroll-view"
        scroll-y
        :style="{ height: wh + 'px' }"
      >
        <view class="cate-lv3-list">
          <view
          v-for="(item2, index2) in categoryList[selectedCategory].children"
          :key="index2"
          class="cate-lv3-item"
          >
            <image :src="item2.image.external_url"></image>
            <text>{{item2.name}}</text>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
import MySearch from '@/components/search.vue'
export default {
  components:{
    MySearch
  },
  data() {
    return {
      wh: 0,
      selectedCategory: 0,
      categoryList: [] //全部分类
    }
  },

  created() {
    // 动态获取窗口可用高度，以便让页面中的左右两块滚动区域的高度撑满屏幕
    // 1. 获取当前系统信息
    const sysInfo = uni.getSystemInfoSync()

    // 2. 从系统信息中获取窗口可用高度并赋值给 wh
    // 窗口的可用高度 = 屏幕高度 - navigationBar高度 - tabBar高度
    this.wh = sysInfo.windowHeight
    this.getCategoryList()
  },

  methods: {
    //获取全部分类列表
    async getCategoryList() {
      const res = await this.$http.get('/category/list')
      if (res.status !== 200) return this.$msg()
      // console.log(res)
      this.categoryList = res.data.list
    },
    // 切换显示效果
    selectCategoryHandler(index) {
      this.selectedCategory = index
    },
    // 跳转搜索页
    goToSearchPage(){
        // console.log('去搜索页')
         uni.navigateTo({
            url:'/pages_sub1/search/search'
        })
    }
  }
}
</script>

<style scoped lang="scss">
.scroll-view-container {
  display: flex;

  .left-scroll-view {
    width: 240rpx;

    .left-scroll-view-item {
      height: 90rpx;
      line-height: 90rpx;
      background-color: #f7f7f7;
      text-align: center;
      font-size: 28rpx;

      // 激活项的样式
      &.active {
        background-color: #ffffff;
        position: relative;
        color: #c00000;

        // 渲染激活项左侧的红色指示边线
        &::before {
          content: ' ';
          display: block;
          width: 6rpx;
          height: 60rpx;
          // background-color: #c00000;
          position: absolute;
          left: 0;
          top: 50%;
          transform: translateY(-50%);
        }
      }
    }
  }
  .cate-lv3-list {
  display: flex;
  flex-wrap: wrap;

  .cate-lv3-item {
    width: 33.33%;
    margin-bottom: 46rpx;
    display: flex;
    flex-direction: column;
    align-items: center;

    image {
      width: 146rpx;
      height: 146rpx;
    }

    text {
      font-size: 26rpx;
    }
  }
}
}

</style>
